import { SUCCESS_CODE } from '@/utils/request';
import { getTimeRangeUnit, rangePickerValueFomatter } from '@/utils/time';
import moment from 'moment';
import { message } from 'antd';
import { request } from 'umi';

// 今日应收金额
export async function fetchAmountReceivableToday() {

  const resReceivable = await request('/data/statistics/point/amount/receivable');

  if (resReceivable?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ resReceivable.message || '未知错误' }`);

    return null;

  }

  return { all: resReceivable.data[0] };
}

// 今日实收金额
export async function fetchAmountReceivedToday() {

  const resReceived = await request('/data/statistics/point/amount/received');

  if (resReceived?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ resReceived.message || '未知错误' }`);

    return null;

  }

  return { all: resReceived.data[0] };
}

// 今日待收金额
export async function fetchAmountUnCollectedToday() {

  const resUnCollected = await request('/data/statistics/point/amount/unreceived');

  if (resUnCollected?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ resUnCollected.message || '未知错误' }`);

    return null;

  }

  return { all: resUnCollected.data[0] };
}

// 今日停车总数
export async function fetchParkToday() {

  const resPark = await request('/data/statistics/parking/point');

  if (resPark?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ resPark.message || '未知错误' }`);

    return null;

  }

  return { all: resPark.data[0] };
}


// 根据时间范围获取停车数 //unit:params.range 粒度
// 1 表示泊入车次

export async function fetchRangeParkIn(params: any) {
  const filter = rangePickerValueFomatter(params.tick).map(t => moment(t).format('YYYY-MM-DD HH:mm:ss'));

  const filterUnit = getTimeRangeUnit(params.tick);

  const rangePark = await request('/data/statistics/parking', {

    params: { startDate: filter[0], endDate: filter[1], state: 1, unit: filterUnit.unit },

  });

  if (rangePark?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ rangePark.message || '未知错误' }`);

    return null;

  }

  return { allIn: rangePark.data.points };
}

// 2 表示泊出车次
export async function fetchRangeParkOut(params: any) {

  const filter = rangePickerValueFomatter(params.tick).map(t => moment(t).format('YYYY-MM-DD HH:mm:ss'));

  const filterUnit = getTimeRangeUnit(params.tick);

  const rangePark = await request('/data/statistics/parking', {

    params: { startDate: filter[0], endDate: filter[1], state: 2, unit: filterUnit.unit },

  });

  if (rangePark?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ rangePark.message || '未知错误' }`);

    return null;

  }

  return { allOut: rangePark.data.points };
}


export async function fetchPickerChangeIn(params: any) {

  const filter = rangePickerValueFomatter(params.tick).map(t => moment(t).format('YYYY-MM-DD HH:mm:ss'));

  const rangePark = await request('/data/statistics/parking', {

    params: { startDate: filter[0], endDate: filter[1], state: 1, unit: params.range },

  });

  if (rangePark?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ rangePark.message || '未知错误' }`);

    return null;

  }

  return { allIn: rangePark.data.points };
}


export async function fetchPickerChangeOut(params: any) {

  const filter = rangePickerValueFomatter(params.tick).map(t => moment(t).format('YYYY-MM-DD HH:mm:ss'));

  const rangePark = await request('/data/statistics/parking', {

    params: { startDate: filter[0], endDate: filter[1], state: 2, unit: params.range },

  });

  if (rangePark?.code !== SUCCESS_CODE) {

    message.error(`获取失败 ${ rangePark.message || '未知错误' }`);

    return null;

  }

  return { allOut: rangePark.data.points };
}







